<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="项目名称" prop="projectCode">
      <el-select
          @change="changeCode"
          style="width:100%"
          v-model="dataForm.projectCode"
          placeholder="请选择项目"
        >
          <el-option
            v-for="item in optionsprojectId"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="人员" prop="personnelId">
      <el-select
          style="width:100%"
          v-model="dataForm.personnelId"
          placeholder="请选择人员"
        >
          <el-option
            v-for="item in optionspersonnelName"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="数据类型" prop="dataType">
      <el-select
          style="width:100%"
          v-model="dataForm.dataType"
          placeholder="请选择数据类型"
        >
          <el-option
            v-for="item in optionsentryExit"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="行为内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="行为内容"></el-input>
    </el-form-item>
    <el-form-item label="发生时间" prop="happenDate">
      <el-date-picker
          style="width:100%"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.happenDate"
          type="datetime"
          placeholder="选择日期"
        >
        </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        optionspersonnelName:[],
        optionsprojectId:[],
        optionsentryExit:[],
        visible: false,
        dataForm: {
          id: 0,
          projectCode: '',
          personnelId: '',
          dataType: '',
          content: '',
          happenDate: ''
        },
        dataRule: {
          projectCode: [
            { required: true, message: '项目编号不能为空', trigger: 'blur' }
          ],
          personnelId: [
            { required: true, message: '人员id不能为空', trigger: 'blur' }
          ],
          dataType: [
            { required: true, message: '数据类型：1良好行为、2不良行为不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '行为内容不能为空', trigger: 'blur' }
          ],
          happenDate: [
            { required: true, message: '发生时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
         // 选择项目
    changeCode(e) {
      //获取人员
      this.$http({
        url: this.$http.adornUrl(
          `/sys/getParamenters/getPersonnelProfile?projectCode=${e}`
        ),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionspersonnelName = data.list;
        }
      });
    },
    // 获取项目
    getCompanyTypes() {
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getProjectList`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsprojectId = data.list;
        }
      });

      //获取数据类型
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getSincerityDataType`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsentryExit = data.list;
        }
      });
    },
      init (id) {
        this.getCompanyTypes()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bsincerityinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.projectCode = data.bSincerityInfo.projectCode
                this.changeCode(data.bSincerityInfo.projectCode)
                this.dataForm.personnelId = data.bSincerityInfo.personnelId
                this.dataForm.dataType = data.bSincerityInfo.dataType
                this.dataForm.content = data.bSincerityInfo.content
                this.dataForm.happenDate = data.bSincerityInfo.happenDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bsincerityinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'projectCode': this.dataForm.projectCode,
                'personnelId': this.dataForm.personnelId,
                'dataType': this.dataForm.dataType,
                'content': this.dataForm.content,
                'happenDate': this.dataForm.happenDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
